package com.sxhnpg.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.sxhnpg.constant.Constants;
import com.sxhnpg.exception.SxhnpgException;
import com.sxhnpg.manager.UserDetailManager;
import com.sxhnpg.manager.UserManager;
import com.sxhnpg.model.User;
import com.sxhnpg.model.UserDetail;
import com.sxhnpg.util.DigestUtil;


public class UserAction extends BaseAction{

	private String userid;
	private String password;
	private String username;
	private int suspend;
	private int permission;
	private String createUser;
	private Date createTime;
	private Date lastLoginTime;
	
	private boolean ok = false;	// 用户Id是否使用
	
	private UserDetail userDetail;

	// 用户管理器
	private UserManager userManager;
	
	// 用户详细信息管理器
	private UserDetailManager userDetailManager;
	
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public void setUserDetailManager(UserDetailManager userDetailManager) {
		this.userDetailManager = userDetailManager;
	}
	
	private static Logger logger = Logger.getLogger(UserAction.class);  
	
	// 用户信息列表
	public String userList() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			List<User> userList = new ArrayList<User>(); 
			userList = userManager.findUserList();
			ServletActionContext.getRequest().setAttribute("userList", userList);
		}catch(Exception e) {
			logger.error("查看用户列表出错：",e);
			throw new SxhnpgException();
		}
		return "list";
	}
	
	// 添加用户
	public String addUser() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			if(userInSession.getPermission() != 1){
				return null;
			}
			User user = new User();
			user.setUserid(userid);
			user.setUsername(username);
			user.setPassword(DigestUtil.md5Digest(password));
			user.setSuspend(suspend);
			user.setPermission(permission);
			user.setCreateUser(userInSession.getUserid());
			user.setCreateTime(new Date());
			
			UserDetail userDetail_new = new UserDetail();
			userDetail_new.setUserid(userid);
			userDetail_new.setEmail(userDetail.getEmail());
			userDetail_new.setMobile(userDetail.getMobile());
			userDetail_new.setSexual(userDetail.getSexual());
			userDetail_new.setTitle(userDetail.getTitle());
			
			userManager.save(user);
			userDetailManager.save(userDetail_new);
			
			ServletActionContext.getRequest().setAttribute(Constants.OPT_SUCCESS_INFO, "添加用户信息成功！");
		}catch(Exception e) {
			logger.error("添加用户出错：",e);
			throw new SxhnpgException();
		}
		return "result";
	}
	
	// 修改基本用户信息
	public String updateUserInfo() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			
			UserDetail userDetail_new = userDetailManager.find(userInSession.getUserid()); 
			userDetail_new.setMobile(userDetail.getMobile());
			userDetail_new.setEmail(userDetail.getEmail());
			userDetail_new.setSexual(userDetail.getSexual());
			
			User user=userManager.findUser(userInSession.getUserid());
			user.setUsername(username);
			user.setUserDetail(userDetail_new);
			
			userDetailManager.update(userDetail_new);
			userManager.update(user);
			
			session.put(Constants.LOGIN_USER_INFO, user);
			
			ServletActionContext.getRequest().setAttribute(Constants.OPT_SUCCESS_INFO, "修改用户信息成功！");
			
			return "result";
		}catch (Exception e) {
			logger.error("用户登录出错：",e);
			throw new SxhnpgException();
		}
	}
	
	// 修改基本用户信息
	public String updatePwd() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			
			HttpServletRequest request = ServletActionContext.getRequest();
			String passwordOld = request.getParameter("passwordOld");
			passwordOld = DigestUtil.md5Digest(passwordOld);
			if(!userInSession.getPassword().equals(passwordOld)){
				request.setAttribute(Constants.OPT_ERROR_INFO, "输入的原密码不正确！");
			}else{
				userInSession.setPassword(DigestUtil.md5Digest(password));
				userManager.update(userInSession);
				request.setAttribute(Constants.OPT_SUCCESS_INFO, "修改密码成功！");
				session.put(Constants.LOGIN_USER_INFO, userInSession);
			}
			return "result";
			
		}catch (Exception e) {
			logger.error("用户登录出错：",e);
			throw new SxhnpgException();
		}
	}
	
	// 检测账号是否可用
	public String checkUserid() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			User user = userManager.findUser(userid);
			if(user == null){
				ok = 	true;
			}else{
				ok = 	false;
			}
		}catch(Exception e) {
			ok = false;
			logger.error("检测用户id出错：",e);
			throw new SxhnpgException();
		}
		return "check_userid";
	}
	
	// 删除用户
	public String removeUser() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			if(userInSession.getPermission() != 1){
				return null;
			}
			User user = userManager.findUser(userid);
			if(user != null){
				userDetailManager.remove(user.getUserDetail());
				userManager.remove(user);
				ServletActionContext.getRequest().setAttribute(Constants.OPT_SUCCESS_INFO, "用户删除成功！");
			}
		}catch(Exception e) {
			logger.error("删除用户出错：",e);
			throw new SxhnpgException();
		}
		return "result";
	}
	
	// 跳转修改页面
	public String goModifyPage() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			if(userInSession.getPermission() != 1){
				return null;
			}
			User user = userManager.findUser(userid);
			
			// 用于进度条判断
			int progressCount = 0;
			if(!"".equals(user.getUserDetail().getEmail()) && user.getUserDetail().getEmail() != null){
				progressCount += 10;
			}
			if(!"".equals(user.getUserDetail().getMobile()) && user.getUserDetail().getMobile() != null){
				progressCount += 10;
			}
			
			ServletActionContext.getRequest().setAttribute("progressCount", progressCount);
			ServletActionContext.getRequest().setAttribute("user", user);
			
		}catch(Exception e) {
			logger.error("跳转修改页面失败：",e);
			throw new SxhnpgException();
		}
		return "modifyPage";
	}
	
	// 修改用户
	public String modifyUser() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			if(userInSession.getPermission() != 1){
				return null;
			}
			User user = userManager.findUser(userid);
			user.setUsername(username);
			user.setPassword(DigestUtil.md5Digest(password));
			user.setSuspend(suspend);
			user.setPermission(permission);
			
			UserDetail userDetail_new = userDetailManager.find(userid);
			userDetail_new.setEmail(userDetail.getEmail());
			userDetail_new.setMobile(userDetail.getMobile());
			userDetail_new.setSexual(userDetail.getSexual());
			userDetail_new.setTitle(userDetail.getTitle());
			
			userDetailManager.update(userDetail_new);
			userManager.update(user);
			
			ServletActionContext.getRequest().setAttribute(Constants.OPT_SUCCESS_INFO, "修改用户信息成功！");
			
		}catch(Exception e) {
			logger.error("修改用户出错：",e);
			throw new SxhnpgException();
		}
		return "result";
	}
	
	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getSuspend() {
		return suspend;
	}

	public void setSuspend(int suspend) {
		this.suspend = suspend;
	}

	public int getPermission() {
		return permission;
	}

	public void setPermission(int permission) {
		this.permission = permission;
	}

	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public UserDetail getUserDetail() {
		return userDetail;
	}

	public void setUserDetail(UserDetail userDetail) {
		this.userDetail = userDetail;
	}
	public boolean isOk() {
		return ok;
	}
	public void setOk(boolean ok) {
		this.ok = ok;
	}
	
	
}
